varying vec3 normedPosition;
varying vec3 worldPosition;

uniform vec3 centers0[10];
uniform vec4 classes0[10];
uniform float strengths0[10];

uniform vec3 centers1[120];
uniform vec4 classes1[120];
uniform float strengths1[2];


int classify0 (vec3 pos) {
	float dMin = 999999.0;
	float d    = 999999.0;
	int found = -1;
	for (int i=0; i<10; i++) {
		d = abs(distance(pos, centers0[i].xyz)) / strengths0[int(classes0[i].a)];
		if (d<dMin) {
			dMin = d;
			found = i;
		}
	}
	return found;
}

int classify1 (vec3 pos) {
	float dMin = 999999.0;
	float d    = 999999.0;
	int found = -1;
	for (int i=0; i<60; i++) {
		d = abs(distance(pos, centers1[i].xyz)) * strengths1[int(classes1[i].a)];
		if (d<dMin) {
			dMin = d;
			found = i;
		}
	}
	return found;
}

bool isEdge() {
	float thresh = 0.01;
	return (normedPosition.x>(1.0-thresh) && normedPosition.y>(1.0-thresh) ||
			normedPosition.x>(1.0-thresh) && normedPosition.y<thresh || 
			normedPosition.x<(thresh) && normedPosition.y>(1.0-thresh) ||
			normedPosition.x<(thresh) && normedPosition.y<thresh ||
			
			normedPosition.x>(1.0-thresh) && normedPosition.z>(1.0-thresh) ||
			normedPosition.x>(1.0-thresh) && normedPosition.z<thresh || 
			normedPosition.x<(thresh) && normedPosition.z>(1.0-thresh) ||
			normedPosition.x<(thresh) && normedPosition.z<thresh ||
			
			normedPosition.y>(1.0-thresh) && normedPosition.z>(1.0-thresh) ||
			normedPosition.y>(1.0-thresh) && normedPosition.z<thresh || 
			normedPosition.y<(thresh) && normedPosition.z>(1.0-thresh) ||
			normedPosition.y<(thresh) && normedPosition.z<thresh
			);
}
void main() {

	vec3 c = gl_TexCoord[0].xyz;
	
	//c = classes0[classify0(worldPosition)].xyz;
	c = classes1[classify1(worldPosition)].xyz;
	
	if (isEdge()) {
		c = vec3(0,0,0);
	}
	
	gl_FragColor = vec4(c,1.0); 
}